package com.ruoyi.common.utils;

import lombok.extern.log4j.Log4j2;

import java.util.concurrent.Semaphore;

/**
 * @Author wy
 * @Description JAVA并发信号量工具类
 * @Date 2023/11/27 14:13
 * @Version 1.0
 */
@Log4j2
public class SemaphoreUtils {

    /**
     * 获取信号量
     *
     * @param semaphore
     * @return
     */
    public static boolean tryAcquire(Semaphore semaphore)
    {
        boolean flag = false;

        try
        {
            flag = semaphore.tryAcquire();
        }
        catch (Exception e)
        {
            log.error("获取信号量异常", e);
        }

        return flag;
    }

    /**
     * 释放信号量
     *
     * @param semaphore
     */
    public static void release(Semaphore semaphore)
    {

        try
        {
            semaphore.release();
        }
        catch (Exception e)
        {
            log.error("释放信号量异常", e);
        }
    }
}
